using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using System.Data.SqlClient;
using System.Configuration;
using BUS;
using DTO;
using Utility;
public partial class admin_A_ChiTietSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Loaddata();
            Loadhinh();

        }
    }
    public Object soluonghinh()
    {
        int id = int.Parse(Request.QueryString["id"]);
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
        cn.Open();

        SqlCommand cmd = new SqlCommand("select count(*) from hinhanh where mamh='" + id + "'", cn);
        return cmd.ExecuteScalar();


    }
    public void Loaddata()
    {
        Criteria cri = new Criteria();
        //int total=0;
        //PagerV2_8_1.PageSize = 1;
        int id = int.Parse(Request.QueryString["id"]);
        MatHang_DTO mathang = MATHANG_BUS.GetByID(id);
        lit_MaSP.Text = mathang.MaMH.ToString();
        lit_TenSP.Text = mathang.TenMH;

        LoaiHang_DTO loaihang = LOAIHANG_BUS.GetByID(mathang.MaLoai);
        lit_LoaiSP.Text = loaihang.TenLoai;
        lit_dongia.Text = mathang.GiaBan.ToString();
        lit_slphieuban.Text = mathang.SoLuongPhieuBan.ToString();
        lit_slphieuconlai.Text = mathang.SoLuongPhieuConLai.ToString();
        lit_giamgia.Text = mathang.TiLeGiam.ToString();
        lit_phigiaohang.Text = mathang.PhiGiaoHangTanNoi.ToString();
        lit_chietkhau.Text = mathang.PhanTram_ChietKhau.ToString();
        DoanhNghiep_DTO doanhnghiep = DoanhNghiep_BUS.GetByID(mathang.MaDN);
        lit_DoanhNghiep.Text = doanhnghiep.TenDN;
        KHUVUC_DTO khuvuc = KHUVUC_BUS.GetByID(mathang.MaKhuVuc);
        lit_Khuvuc.Text = khuvuc.TenKhuVuc;
        HinhAnh_DTO hinhanhminhhoa = HinhAnh_BUS.GetByID(mathang.MaMH);

        int soluongimage = int.Parse(soluonghinh(mathang.MaMH).ToString());
        if (soluongimage <= 0)
        {
            img_Hinhanh.ImageUrl = "~/photos/thumb/" + "no_images.jpg";
        }
        else
        {
            img_Hinhanh.ImageUrl = "~/photos/thumb/" + hinhanhminhhoa.Hinh;
        }




        //img_Hinhanh.ImageUrl = "~/photos/" + hinhanhminhhoa.Hinh;
        lit_Batdau.Text = mathang.ThoiGian_BatDau.ToString();
        lit_ketthuc.Text = mathang.ThoiGian_KetThuc.ToString();

        TinhTrang_SanPham_DTO tinhtrang = TinhTrang_SanPham_BUS.GetByID(mathang.MaTinhTrang_SP);
        lit_tinhtrang.Text = tinhtrang.TenTinhTrang;
        lit_mota.Text = mathang.MoTa;
        DoanhNghiep_DTO dnghiep = DoanhNghiep_BUS.GetByID(mathang.MaDN);
        hinhbando.ImageUrl = "~/photos/thumb/" + dnghiep.BanDo;

        //Criteria cri = new Criteria();
        //cri.add(Condition.AND, tbHinhAnhColumns.MaMH, Condition.EQUAL, id);
        //int total = 0;
        //PagerV2_8_1.PageSize = 5;
        //tbHinhAnhCollection hinhclt = HinhAnh_BUS.GetByMaMH(id);
        ////HinhAnh_DTO hinhclt = HinhAnh_BUS.GetByID(id);
        //DataList1.DataSource = hinhclt;
        //DataList1.DataBind();
        //PagerV2_8_1.ItemCount = total;
        ////PagerV2_8_1.ItemCount = total;


    }
    public Object soluonghinh(int mamhang)
    {

        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("select count(mamh) from hinhanh where mamh='" + mamhang + "'", cn);
        return cmd.ExecuteScalar();


    }
    private void Loadhinh()
    {
        int id = int.Parse(Request.QueryString["id"]);
        tbHinhAnhCollection hinhclt = HinhAnh_BUS.GetByMaMH(id);
        DataList1.DataSource = hinhclt;
        DataList1.DataBind();



    }


    protected void PagerV2_8_1_Command(object sender, CommandEventArgs e)
    {

        PagerV2_8_1.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        Loaddata();
    }


    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            int id = int.Parse(Request.QueryString["id"]);
            HinhAnh_DTO hinh = (HinhAnh_DTO)e.Item.DataItem;
            Image hinhanh = (Image)e.Item.FindControl("hinhanh");
            hinhanh.ImageUrl = "~/photos/thumb/" + hinh.Hinh;

          
            
           

        }
    }
    
}
